package com.ds.dsm.view.config.nav.tab.stacks;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.config.ResultModel;
import com.ds.dsm.view.config.form.field.FieldFormInfo;
import com.ds.dsm.view.config.nav.buttonviews.NavButtonViewsInfoGroup;
import com.ds.dsm.view.config.nav.items.ModuleItemsInfo;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.tab.NavTabsViewBean;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.nav.annotation.NavGroupViewAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.dsm.view.field.FieldFormConfig;
import com.ds.esd.dsm.view.field.FieldModuleConfig;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.esd.tool.ui.enums.Dock;
import com.ds.esd.tool.ui.enums.VAlignType;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(path = "/dsm/view/config/nav/stacks/")
@MethodChinaName(cname = "配置信息")
@ButtonViewsAnnotation(barLocation = BarLocationType.left, barVAlign = VAlignType.top)
public class NavStacksConfigView {
    @CustomAnnotation(uid = true, hidden = true)
    private String esdclass;
    @CustomAnnotation(pid = true, hidden = true)
    private String projectId;
    @CustomAnnotation(pid = true, hidden = true)
    private String viewInstId;
    @CustomAnnotation(pid = true, hidden = true)
    private String domainId;
    public NavStacksConfigView() {

    }


    @RequestMapping(method = RequestMethod.POST, value = "StacksInfoGroup")
    @NavGroupViewAnnotation(reSetUrl = "clear", saveUrl = "updateStacksBase", autoSave = true)
    @ModuleAnnotation(dock = Dock.fill, caption = "Folding配置", index = 0, imageClass = "spafont spa-icon-project")
    @ResponseBody
    public ResultModel<NavButtonViewsInfoGroup> getStacksInfoGroup(String serviceClassName, String methodName,String domainId, String viewInstId) {
        ResultModel<NavButtonViewsInfoGroup> result = new ResultModel<NavButtonViewsInfoGroup>();
        return result;
    }


    @RequestMapping(method = RequestMethod.POST, value = "FieldFormList")
    @GridViewAnnotation
    @ModuleAnnotation( imageClass = "spafont spa-icon-c-comboinput", caption = "表单字段")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<FieldFormInfo>> getFieldFormList(String sourceClassName, String methodName,String domainId, String viewInstId) {
        ListResultModel<List<FieldFormInfo>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = config.getCurrConfig().getMethodByName(methodName);
            NavTabsViewBean navTabsViewBean = (NavTabsViewBean) customMethodAPIBean.getView();
            List<String> fieldNames = navTabsViewBean.getFieldNames();
            List<FieldFormConfig> fields = new ArrayList<>();
            for (String fieldName : fieldNames) {
                FieldFormConfig fieldFormConfig = (FieldFormConfig) navTabsViewBean.getFieldConfigMap().get(fieldName);
                if (fieldFormConfig != null) {
                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, FieldFormInfo.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    @RequestMapping(method = RequestMethod.POST, value = "ModuleList")
    @GridViewAnnotation
    @ModuleAnnotation( imageClass = "spafont spa-icon-conf", caption = "模块列表")
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<ModuleItemsInfo>> getModuleList(String sourceClassName, String methodName,String domainId, String viewInstId) {
        ListResultModel<List<ModuleItemsInfo>> cols = new ListResultModel();
        try {
            ViewEntityConfig config = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,viewInstId);
            MethodConfig customMethodAPIBean = config.getCurrConfig().getMethodByName(methodName);
            NavTabsViewBean navTabsViewBean = (NavTabsViewBean) customMethodAPIBean.getView();
            List<FieldModuleConfig> items = navTabsViewBean.getItems();
            List<FieldModuleConfig> fields = new ArrayList<>();
            for ( FieldModuleConfig fieldFormConfig : items) {
                if (fieldFormConfig != null) {
                    fieldFormConfig.setSourceClassName(sourceClassName);
                    fields.add(fieldFormConfig);
                }
            }
            cols = PageUtil.getDefaultPageList(fields, ModuleItemsInfo.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return cols;
    }

    public String getDomainId() {
        return domainId;
    }

    public void setDomainId(String domainId) {
        this.domainId = domainId;
    }

    public String getEsdclass() {
        return esdclass;
    }

    public void setEsdclass(String esdclass) {
        this.esdclass = esdclass;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }
}
